package cc.admin.modules.graduate.service.impl;

import cc.admin.common.api.vo.Result;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.common.util.oConvertUtils;
import cc.admin.modules.graduate.entity.*;
import cc.admin.modules.graduate.mapper.*;
import cc.admin.modules.graduate.service.IDesignQuestionsListService;
import cc.admin.modules.graduate.entity.DTO.DesignQuestionsListDto;
import cc.admin.poi.excel.ExcelExportUtil;
import cc.admin.poi.excel.ExcelImportUtil;
import cc.admin.poi.excel.def.NormalExcelConstants;
import cc.admin.poi.excel.entity.ExportParams;
import cc.admin.poi.excel.entity.ImportParams;
import cc.admin.poi.excel.export.ExcelExportServer;
import cc.admin.poi.excel.view.PoiEntityExcelView;
import cc.admin.poi.util.ExcelUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

/**
 * @Description: 算法设计与分析题主表
 * @Author: cc-admin
 * @Date: 2025-01-03
 * @Version: V1.0.0
 */
@Service
@Slf4j
public class DesignQuestionsListServiceImpl extends ServiceImpl<DesignQuestionsListMapper, DesignQuestionsListDto> implements IDesignQuestionsListService {

    @Autowired
    private DesignQuestionsServiceImpl designQuestionsService;
    @Autowired
    private DesignAnswerListMapper designAnswerListMapper;
    @Autowired
    private DesignAnswerMapper designAnswerMapper;

    @Autowired
    private DesignQuestionsMapper designQuestionsMapper;
    @Autowired
    private DesignQuestionsListMapper designQuestionsListMapper;

    @Autowired
    private IDesignQuestionsListService designQuestionsListService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private TopicMapper topicMapper;

    @Override
    public String saveDesignQuestions(DesignQuestionsListDto designQuestionsList, String parentId) {
        ArrayList<String> childIds = new ArrayList<>();
        designQuestionsList.getQuestionList().forEach(question -> {
            String childId = IdUtil.objectId();
            question.setId(childId);
            question.setParentId(parentId);
            designQuestionsService.save(question);
            childIds.add(childId);
        });

        return String.join(",", childIds);
    }

    @Override
    public List<DesignQuestions> getchildQuestions(String childIds) {
        ArrayList<DesignQuestions> designQuestionsList = new ArrayList<>();
        String[] childIdArr = childIds.split(",");
        for (int i = 0; i < childIdArr.length; i++) {
            DesignQuestions designQuestion = designQuestionsService.getById(childIdArr[i]);
            designQuestionsList.add(designQuestion);
        }
        return designQuestionsList;
    }

    @Override
    @Transactional
    public void updateChild(DesignQuestionsListDto designQuestionsList) {
        LambdaQueryWrapper<DesignQuestions> designQuestionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String parentId = designQuestionsList.getId();
        designQuestionsLambdaQueryWrapper.eq(parentId != null && !parentId.equals(""), DesignQuestions::getParentId, parentId);
        designQuestionsService.remove(designQuestionsLambdaQueryWrapper);
        //把新的题目更新进去
        ArrayList<String> childIds = new ArrayList<>();
        designQuestionsList.getQuestionList().forEach(question -> {
            String oldId = question.getId();
            String childId = IdUtil.objectId();
            question.setId(childId);
            question.setParentId(parentId);
            designQuestionsService.save(question);
            childIds.add(childId);

            LambdaUpdateWrapper<DesignAnswer> updateWrapper2 = new LambdaUpdateWrapper<DesignAnswer>().eq(DesignAnswer::getQuestionId, oldId);
            DesignAnswer designAnswer2 = new DesignAnswer();
            designAnswer2.setQuestionId(childId);
            designAnswerMapper.update(designAnswer2, updateWrapper2);
            //修改学生错题的讲解视频
            if (question.getViedoUrl() != null && !question.getViedoUrl().equals("")) {
                String analysisVideo = question.getViedoUrl();
                LambdaUpdateWrapper<DesignAnswer> updateWrapper = new LambdaUpdateWrapper<DesignAnswer>().eq(DesignAnswer::getQuestionId, childId);
                DesignAnswer designAnswer = new DesignAnswer();
                designAnswer.setAnalysisVideo(analysisVideo);
                designAnswer.setQuestionId(childId);
                designAnswerMapper.update(designAnswer, updateWrapper);
            }
        });
        if (childIds != null && childIds.size() >= 0) {
            designQuestionsList.setChildIds(String.join(",", childIds));

        } else {
            designQuestionsList.setChildIds("");
        }
        this.updateById(designQuestionsList);
    }

    @Override
    @Transactional
    public void removeselfAndChild(String parentId) {
        //删除子表数据
        LambdaQueryWrapper<DesignQuestions> designQuestionsLambdaQueryWrapper = new LambdaQueryWrapper<>();

        designQuestionsLambdaQueryWrapper.eq(parentId != null && !parentId.equals(""), DesignQuestions::getParentId, parentId);
        designQuestionsService.remove(designQuestionsLambdaQueryWrapper);
        //删除自己
        this.removeById(parentId);
    }

    @Override
    public List<DesignAnswerList> getErrorDesign(String tab, String key) {
        LambdaQueryWrapper<DesignAnswerList> answerListWrapper = new LambdaQueryWrapper<>();
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        answerListWrapper.eq(DesignAnswerList::getUserName, sysUser.getRealname());
        HashSet<DesignAnswerList> errorDesignSet = new HashSet<>();
        answerListWrapper.eq(DesignAnswerList::getSubjectId, tab);
        answerListWrapper.like(key != null && !key.equals(""), DesignAnswerList::getQuestionStem, key);
        answerListWrapper.orderByAsc(DesignAnswerList::getMajorId);
        List<DesignAnswerList> designAnswerLists = designAnswerListMapper.selectList(answerListWrapper);
//        designAnswerLists.forEach(designAnswerList -> {
//            designAnswerList.getQuestionList().forEach(designAnswer -> {
//                if (designAnswer.getShape().equals("0")) {
//                    errorDesignSet.add(designAnswerList);
//                }
//            });
//        });
        designAnswerLists.forEach(designAnswerList -> {
//            String childIds = designAnswerList.getChildIds();
            List<DesignAnswer> designAnswers = new ArrayList<>();
//            if (childIds != "" && childIds != null) {
//                List<String> designAnswerIdList = Arrays.stream(childIds.split(",")).collect(Collectors.toList());
//                designAnswers = designAnswerMapper.selectBatchIds(designAnswerIdList);
            LambdaQueryWrapper<DesignAnswer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DesignAnswer::getParentId, designAnswerList.getId());
            queryWrapper.orderByAsc(DesignAnswer::getIndexNum);
            designAnswers = designAnswerMapper.selectList(queryWrapper);
            designAnswers.forEach(designAnswer -> {
                if (designAnswer.getShape() != null && designAnswer.getShape().equals("0")) {
                    errorDesignSet.add(designAnswerList);
                }
            });
//            }
            designAnswerList.setQuestionList(designAnswers);
        });
//        List<DesignAnswerList> list = new ArrayList<>(errorDesignSet);

        return designAnswerLists;
    }

    @Override
    public List<DesignQuestionsListDto> getNoVideoDesign(String tab) {

        List<Map<String, Object>> mapList = jdbcTemplate.queryForList("SELECT question_id, COUNT(*) AS question_count FROM design_answer  where subject_id = ? and (analysis_video is null or analysis_video = '') GROUP BY question_id ORDER BY question_count desc", tab);

        //父级应用题id集合
        HashSet<String> designQuestionParentIdHashSet = new HashSet<>();
        //遍历没有video的应用题
        mapList.forEach(map -> {
            String questionId = (String) map.get("question_id");
            LambdaQueryWrapper<DesignQuestions> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DesignQuestions::getId, questionId);
            DesignQuestions designQuestions = designQuestionsMapper.selectOne(queryWrapper);
            designQuestionParentIdHashSet.add(designQuestions.getParentId());
        });


        List<DesignQuestionsListDto> errorDesign = new ArrayList<>();
        designQuestionParentIdHashSet.forEach(id -> {

            DesignQuestionsListDto designQuestionsListDto = designQuestionsListMapper.selectById(id);
            //把问题集合放入对象
            List<DesignQuestions> designQuestionsList = designQuestionsListService.getchildQuestions(designQuestionsListDto.getChildIds());
            designQuestionsListDto.setQuestionList(designQuestionsList);
            //把问题分类放对象
            ArrayList<Category> categories = new ArrayList<>();
            //当存在下拉数据时
            if (designQuestionsListDto.getCategoryIds() != null && designQuestionsListDto.getCategoryIdsText() != null) {
                String[] split = designQuestionsListDto.getCategoryIds().split(",");
                String[] split2 = designQuestionsListDto.getCategoryIdsText().split(",");
                for (int i = 0; i < split.length; i++) {
                    Category category = new Category(split[i], split2[i]);
                    categories.add(category);
                }
            }
            designQuestionsListDto.setCategorys(categories);
            errorDesign.add(designQuestionsListDto);
        });


        return errorDesign;
    }

    @Override
    @Transactional
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response, Class<DesignQuestionsListDto> designQuestionsListDtoClass) {
        LambdaQueryWrapper<Topic> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.isNotNull(Topic::getParentId);
        queryWrapper1.ne(Topic::getParentId, "");
        // 查询数据并封装成 Map
        Map<String, String> resultMap = topicMapper.selectList(queryWrapper1).stream()
                .collect(Collectors.toMap(Topic::getLabel, Topic::getValue, (existing, replacement) -> existing, HashMap::new));
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                Workbook workbook = null;
                InputStream fileInputStream = file.getInputStream();
                String fileName = file.getOriginalFilename();
                if (fileName.endsWith("xlsx")) {
                    workbook = new XSSFWorkbook(fileInputStream);// Excel 2007
                } else if (fileName.endsWith("xls")) {
                    workbook = new HSSFWorkbook(fileInputStream);// Excel 2003
                }
                Sheet sheet = workbook.getSheetAt(0);

                // 存储主表数据
                Map<String, DesignQuestionsListDto> masterMap = new HashMap<>();


                // 读取表头
                Row headerRow = sheet.getRow(0);

                for (int i = 3; i <= sheet.getLastRowNum(); i++) {
                    // 存储子表id数据
                    List<String> detailIdList = new ArrayList<>();
                    Row row = sheet.getRow(i);
                    Cell masterSubjectTextCell = row.getCell(0); // 主表 科目名称 在第一列
                    Cell masterQuestionStemCell = row.getCell(1); // 主表题干在第二列
                    Cell masterMajorTextCell = row.getCell(2); // 主表专业在第三列


                    String masterSubjectText = masterSubjectTextCell.getStringCellValue();
                    String masterQuestionStem = masterQuestionStemCell.getStringCellValue();
                    String masterMajorText = masterMajorTextCell.getStringCellValue();

                    String id = UuidUtils.getUUID();    //主表id
                    // 处理主表数据
                    if (!masterMap.containsKey(id)) {
                        DesignQuestionsListDto master = new DesignQuestionsListDto();
                        master.setId(id);
                        master.setSubjectText(masterSubjectText);
                        master.setQuestionStem(masterQuestionStem);
                        master.setMajorText(masterMajorText);
                        String masterMajorId = resultMap.get(masterMajorText);
                        String masterSubjectId = ChoiceQuestionsServiceImpl.topicMap.get(masterSubjectText);
                        master.setSubjectId(masterSubjectId);
                        master.setMajorId(masterMajorId);
                        masterMap.put(id, master);
                    }

                    // 处理子表数据
                    int colIndex = 3;   //规定子表数据从第四列开始
                    int indexNum = 1;   //还需要给子表设置indexNum字段
                    while (true) {
                        String chileId = UuidUtils.getUUID();//子表id

                        //Cell detailSubjectTextCell = row.getCell(colIndex); // 子表科目在第四列
                        Cell detailQuestionCell = row.getCell(colIndex); // 子表题目在第五列
                        Cell detailAnswerCell = row.getCell(++colIndex); // 子表答案在第六列
                        Cell detailAnalysisCell = row.getCell(++colIndex); // 子表解析在第七列
                        //Cell detailMajorTextCell = row.getCell(++colIndex); // 子表专业在第八列
                        //判断有没有数据 CellType.BLANK：空白型单元格
                        if ((detailQuestionCell == null) || (detailQuestionCell.getCellTypeEnum() == CellType.BLANK)) {
                            break;
                        }
                        String detailSubjectText = masterSubjectText;
                        String detailQuestion = detailQuestionCell.getStringCellValue();
                        String detailAnswer = detailAnswerCell.getStringCellValue();
                        String detailAnalysis = detailAnalysisCell.getStringCellValue();
                        String detailMajorText = masterMajorText;
                        DesignQuestions detail = new DesignQuestions();
                        detail.setId(chileId);
                        detail.setParentId(id);
                        detail.setSubjectText(detailSubjectText);
                        detail.setQuestion(detailQuestion);
                        detail.setAnswer(detailAnswer);
                        detail.setAnalysis(detailAnalysis);
                        detail.setMajorText(detailMajorText);
                        String masterMajorId = resultMap.get(masterMajorText);
                        String masterSubjectId = ChoiceQuestionsServiceImpl.topicMap.get(masterSubjectText);
                        detail.setSubjectId(masterSubjectId);
                        detail.setMajorId(masterMajorId);
                        detail.setIndexNum(indexNum);
                        //子表数据入库
                        designQuestionsMapper.insert(detail);

                        detailIdList.add(chileId);
                        colIndex += 1; //下一个数据
                        indexNum++;
                    }
                    //给主表设置子表id字符串
                    masterMap.get(id).setChildIds(String.join(",", detailIdList));

                }
                //主表数据入库
                masterMap.forEach((k, v) -> {
                    //主表数据入库
                    designQuestionsListMapper.insert(v);
                });
                workbook.close();
                fileInputStream.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request, String DesignQuestionsListIds, String catalog, Class<DesignQuestionsListDto> DesignQuestionsListClass, String title) {

        LambdaQueryWrapper<Topic> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.isNotNull(Topic::getParentId);
        queryWrapper1.ne(Topic::getParentId, "");
        String[] labelArray = topicMapper.selectList(queryWrapper1).stream()
                .map(Topic::getLabel)
                .toArray(String[]::new);
        // Step.1 组装查询条件
        QueryWrapper<DesignQuestionsListDto> queryWrapper = QueryGenerator.initQueryWrapper(new DesignQuestionsListDto(), request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (DesignQuestionsListIds != null && !DesignQuestionsListIds.isEmpty()) {
            queryWrapper.in("id", Arrays.asList(DesignQuestionsListIds.split(",")));
        }else {
            //让他查不着数据
            queryWrapper.eq("id","");
        }
        if (catalog != null && !catalog.isEmpty()) {
            queryWrapper.eq("major_id", catalog);
        }
        // Step.2 获取导出数据
        List<DesignQuestionsListDto> pageList = this.list(queryWrapper);
        List<DesignQuestionsListDto> exportList = null;

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(getId(item))).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }


        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new PoiEntityExcelView() {
            @Override
            protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
                String codedFileName = "临时文件";
                Workbook workbook = null;
                //---update-end-----autor:scott------date:20191016-------for:导出字段支持自定义--------
                String[] exportFields = null;

                Object exportFieldStr = model.get(NormalExcelConstants.EXPORT_FIELDS);
                if (exportFieldStr != null && exportFieldStr != "") {
                    exportFields = exportFieldStr.toString().split(",");
                }
                //---update-end-----autor:scott------date:20191016-------for:导出字段支持自定义--------
                if (model.containsKey(NormalExcelConstants.MAP_LIST)) {
                    List<Map<String, Object>> list = (List<Map<String, Object>>) model.get(NormalExcelConstants.MAP_LIST);
                    if (list.size() == 0) {
                        throw new RuntimeException("MAP_LIST IS NULL");
                    }
                    workbook = ExcelExportUtil.exportExcel((ExportParams) list.get(0).get(NormalExcelConstants.PARAMS), (Class<?>) list.get(0).get(NormalExcelConstants.CLASS), (Collection<?>) list.get(0).get(NormalExcelConstants.DATA_LIST), exportFields);
                    for (int i = 1; i < list.size(); i++) {
                        new ExcelExportServer().createSheet(workbook, (ExportParams) list.get(i).get(NormalExcelConstants.PARAMS), (Class<?>) list.get(i).get(NormalExcelConstants.CLASS), (Collection<?>) list.get(i).get(NormalExcelConstants.DATA_LIST), exportFields);
                    }
                } else {
                    workbook = ExcelExportUtil.exportExcel((ExportParams) model.get(NormalExcelConstants.PARAMS), (Class<?>) model.get(NormalExcelConstants.CLASS), (Collection<?>) model.get(NormalExcelConstants.DATA_LIST), exportFields);
                }
                // 在这里进行自定义操作，例如添加下拉列表
                if (workbook != null && workbook.getNumberOfSheets() > 0) {
                    Sheet targetSheet = workbook.getSheetAt(0);
                    ExcelUtil.addValidationToSheet(workbook, targetSheet, labelArray, 'C', 3, 1000);
                    ExcelUtil.addValidationToSheet(workbook, targetSheet, new Object[]{"计算机", "数学", "政治"}, 'A', 3, 1000);
                }
                if (model.containsKey(NormalExcelConstants.FILE_NAME)) {
                    codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME);
                }
                if (workbook instanceof HSSFWorkbook) {
                    codedFileName += HSSF;
                } else {
                    codedFileName += XSSF;
                }
                if (isIE(request)) {
                    codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
                } else {
                    codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
                }
                response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
                ServletOutputStream out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            }

        });
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, DesignQuestionsListClass);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "模板", sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }


    /*
    * 导出模板
    * */

    @Override
    public ModelAndView exportXlsModel( Class<exportDesignModel> exportDesignModel, String title) {

        LambdaQueryWrapper<Topic> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.isNotNull(Topic::getParentId);
        queryWrapper1.ne(Topic::getParentId, "");
        String[] labelArray = topicMapper.selectList(queryWrapper1).stream()
                .map(Topic::getLabel)
                .toArray(String[]::new);

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new PoiEntityExcelView() {
            @Override
            protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
                String codedFileName = "临时文件";
                Workbook workbook = null;
                //---update-end-----autor:scott------date:20191016-------for:导出字段支持自定义--------
                String[] exportFields = null;

                Object exportFieldStr = model.get(NormalExcelConstants.EXPORT_FIELDS);
                if (exportFieldStr != null && exportFieldStr != "") {
                    exportFields = exportFieldStr.toString().split(",");
                }
                //---update-end-----autor:scott------date:20191016-------for:导出字段支持自定义--------
                if (model.containsKey(NormalExcelConstants.MAP_LIST)) {
                    List<Map<String, Object>> list = (List<Map<String, Object>>) model.get(NormalExcelConstants.MAP_LIST);
                    if (list.size() == 0) {
                        throw new RuntimeException("MAP_LIST IS NULL");
                    }
                    workbook = ExcelExportUtil.exportExcel((ExportParams) list.get(0).get(NormalExcelConstants.PARAMS), (Class<?>) list.get(0).get(NormalExcelConstants.CLASS), (Collection<?>) list.get(0).get(NormalExcelConstants.DATA_LIST), exportFields);
                    for (int i = 1; i < list.size(); i++) {
                        new ExcelExportServer().createSheet(workbook, (ExportParams) list.get(i).get(NormalExcelConstants.PARAMS), (Class<?>) list.get(i).get(NormalExcelConstants.CLASS), (Collection<?>) list.get(i).get(NormalExcelConstants.DATA_LIST), exportFields);
                    }
                } else {
                    workbook = ExcelExportUtil.exportExcel((ExportParams) model.get(NormalExcelConstants.PARAMS), (Class<?>) model.get(NormalExcelConstants.CLASS), (Collection<?>) model.get(NormalExcelConstants.DATA_LIST), exportFields);
                }
                // 在这里进行自定义操作，例如添加下拉列表
                if (workbook != null && workbook.getNumberOfSheets() > 0) {
                    Sheet targetSheet = workbook.getSheetAt(0);
                    ExcelUtil.addValidationToSheet(workbook, targetSheet, labelArray, 'C', 3, 1000);
                    ExcelUtil.addValidationToSheet(workbook, targetSheet, new Object[]{"计算机", "数学", "政治"}, 'A', 3, 1000);
                }
                if (model.containsKey(NormalExcelConstants.FILE_NAME)) {
                    codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME);
                }
                if (workbook instanceof HSSFWorkbook) {
                    codedFileName += HSSF;
                } else {
                    codedFileName += XSSF;
                }
                if (isIE(request)) {
                    codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
                } else {
                    codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
                }
                response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
                ServletOutputStream out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            }

        });
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, exportDesignModel);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "模板", "具体内容请按照题目，答案，解析的顺序在后方依次填写", title));
        mv.addObject(NormalExcelConstants.DATA_LIST, new ArrayList<>());
        return mv;
    }


}
